package client.gui.ingame;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import lib.Commands;
import lib.Config;
import lib.map.Map;
import lib.player.Player;
import lib.player.PlayerList;
import lib.scrren.DisplayAble;
import client.TCPClientListener;

//Rename to Maptester?
//or use DoubleFrameBuffer merge similart functions to Screen.
public class GamePanel extends DisplayAble {
	int phase = 0;

	int xGrid = 0;
	int yGrid = 0;
	Map map = null;

	public GraphicalChatInterface chat = null;

	Player player = null;
	public PlayerList playerList = null;
	Image miniMap = null;
	TCPClientListener listener = null;
	DrawScore score;

	/**
	 * This is the default constructor
	 * 
	 * @param graphicsConfiguration
	 */
	public GamePanel(TCPClientListener listener, Map map) {
		setKeyAdapter(new java.awt.event.KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (!chat.chatting)
					if (KeyPressed(e))
						exit();
			}

			@Override
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar() == '\n') {
					if (chat.chatting == false) {
						chat.chatting = true;
						return;
					}
					chat.send();
					chat.resetMsg();
					chat.chatting = false;
				} else if (chat.chatting) {
					chat.appendMsg(e.getKeyChar());
				}
			}
		});
		System.out.println("Your resolution: " + Config.xRes + "x" + Config.yRes);
		this.map = map;
		xGrid = Config.xRes / Config.CollumnCount;
		yGrid = Config.yRes / Config.RowCount;
		this.listener = listener;
		this.chat = new GraphicalChatInterface(listener, Config.xRes, Config.yRes);

		player = listener.player;
		playerList = listener.playerList;
		score = new DrawScore(playerList, listener.score, map.xGrid);
	}

	public String draw(Graphics2D g) {
		phase++;
		g.setFont(Config.spaceFont);
		map.drawPart(g, player, phase);
		playerList.draw(g, player, phase);
		if (miniMap != null)
			g.drawImage(miniMap, map.xGrid, Config.yRes - Config.yRes / 5 - 20, null);

		if (chat != null)
			chat.draw(g);

		player.drawItem(g, phase);
		score.draw(g);
		drawMinimap();
		return "";
	}

	public void drawMinimap() {
		long time = System.currentTimeMillis();
		BufferedImage bufferedImage = new BufferedImage(Config.xRes / 5,
				Config.yRes / 5, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = bufferedImage.createGraphics();
		g2d = (Graphics2D) playerList.drawMiniMap(map.drawMiniMap(g2d, 0.2f),
				0.2f);
		g2d.dispose();
		miniMap = bufferedImage;
		if (Config.debug)
			System.out.println("Minimap drawn in "
					+ (System.currentTimeMillis() - time));
	}

	/**
	 * 
	 * @param e
	 *            The Key which has been pressed
	 * @return true if the game should exit
	 */
	public boolean KeyPressed(KeyEvent e) {
		int k = e.getKeyCode();
		switch (k) {
		case 65:// a
			listener.send(Commands.MOVE, player.nr + " 0");// left
			return false;
		case 87:// w
			listener.send(Commands.MOVE, player.nr + " 2");// up
			return false;
		case 83:// d
			listener.send(Commands.MOVE, player.nr + " 3");// right
			return false;
		case 68:// s
			listener.send(Commands.MOVE, player.nr + " 1");// down
			return false;
		case 32:// space
			listener.send(Commands.MOVE, player.nr + " 4");// down
			return false;
		case 27:// escape
			listener.send(Commands.LEAVE, "GAME");
			return true;
		default:
			System.out.println(k);
			return false;
		}
	}

}
